package spring.cloud.tasks.api.service.impl;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.api.database.entity.NameSpaceZooKeeperClusterMappingDto;
import spring.cloud.tasks.api.database.entity.ZooKeeperClusterDto;
import spring.cloud.tasks.api.database.service.NameSpaceZooKeeperClusterMappingDtoService;
import spring.cloud.tasks.api.database.service.TaskConfigurationDtoService;
import spring.cloud.tasks.api.database.service.ZooKeeperClusterDtoService;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.NameSpaceZooKeeperClusterMapping;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.ZooKeeperCluster;
import spring.cloud.tasks.api.service.NameSpaceZooKeeperClusterMappingService;
import spring.cloud.tasks.api.service.TaskService;
import spring.cloud.tasks.api.service.ZooKeeperClusterService;
import spring.cloud.tasks.common.zookeeper.ZooKeeper;
import spring.cloud.tasks.common.zookeeper.ZooKeeperRepository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Setter
@Getter
public class NameSpaceZooKeeperClusterMappingServiceImpl implements NameSpaceZooKeeperClusterMappingService {


    @Resource
    private ZooKeeperClusterDtoService zooKeeperClusterDtoService;
    @Resource
    private ZooKeeperClusterService zooKeeperService;
    @Resource
    private ZooKeeperRepository zooKeeperRepository;
    @Resource
    private NameSpaceZooKeeperClusterMappingDtoService nameSpaceZooKeeperClusterMappingDtoService;

    @Resource
    private TaskConfigurationDtoService taskConfigurationDtoService;
    @Resource
    private TaskService taskService;


    @Override
    public List<NameSpaceZooKeeperClusterMapping> getNameSpaceZooKeeperClusterMappingList() throws RuntimeException {
        List<NameSpaceZooKeeperClusterMapping> list = new ArrayList<NameSpaceZooKeeperClusterMapping>();
        List<NameSpaceZooKeeperClusterMappingDto> dtoList = nameSpaceZooKeeperClusterMappingDtoService.getEffectiveNameSpaceZooKeeperClusterMappingDtoList();
        if (dtoList != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (NameSpaceZooKeeperClusterMappingDto dto : dtoList) {
                try {
                    if (dto.getIsEffective() == 0) {
                        NameSpaceZooKeeperClusterMapping mapping = new NameSpaceZooKeeperClusterMapping();
                        mapping.setCreatedBy(dto.getCreatedBy());
                        mapping.setCreateDateTime(sdf.format(dto.getCreateDateTime()));
                        mapping.setLastUpdatedBy(dto.getLastUpdatedBy());
                        mapping.setLastUpdateDateTime(sdf.format(dto.getLastUpdateDateTime()));
                        //
                        mapping.setZooKeeperClusterKey(dto.getZooKeeperClusterKey());
                        mapping.setNameSpace(dto.getNameSpaceKey());

                        list.add(mapping);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return list;
    }

    @Override
    public void initNameSpaceZooKeeperClusterMapping() throws RuntimeException {
        try {
            List<ZooKeeperClusterDto> zooKeeperClusterDtoList = zooKeeperClusterDtoService.selectAll();
            if (zooKeeperClusterDtoList != null) {
                for (ZooKeeperClusterDto zooKeeperClusterDto : zooKeeperClusterDtoList) {
                    String zooKeeperClusterKey = zooKeeperClusterDto.getZooKeeperClusterKey();
                    String zooKeeperAddressList = zooKeeperClusterDto.getZooKeeperClusterAddressList();
                    ZooKeeper zooKeeper = zooKeeperService.connectZooKeeper(zooKeeperAddressList, null);
                    CuratorFramework curatorFramework = null;
                    if (zooKeeper != null) {
                        curatorFramework = zooKeeper.getCuratorFramework();
                    }
                    if (curatorFramework != null) { // not offline
                        updateNameSpaceZooKeeperClusterMapping(zooKeeperClusterKey, curatorFramework);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    private void updateNameSpaceZooKeeperClusterMapping(String zooKeeperClusterKey, CuratorFramework curatorFramework) throws Exception {
        try {
            List<String> nameSpaceList = curatorFramework.getChildren().forPath("/");
            if (nameSpaceList != null) {
                for (String nameSpace : nameSpaceList) {
                    if (zooKeeperService.nameSpaceCheck(curatorFramework, nameSpace)) {
                        nameSpaceZooKeeperClusterMappingDtoService.insert(zooKeeperClusterKey, nameSpace);
                    }
                }
            }
        } finally {
            curatorFramework.close();
        }
    }

    @Override
    public List<String> getOnlineZooKeeperClusterKeyList() throws RuntimeException {
        List<String> zooKeeperClusterKeyList = new ArrayList<>();
        List<ZooKeeperCluster> zooKeeperClusterList = zooKeeperService.getZooKeeperClusterList();
        if (zooKeeperClusterList != null) {
            Iterator<ZooKeeperCluster> iterator = zooKeeperClusterList.iterator();
            while (iterator.hasNext()) {
                ZooKeeperCluster next = iterator.next();
                if (!next.isOffline()) {
                    zooKeeperClusterKeyList.add(next.getZooKeeperClusterKey());
                }
            }
        }
        return zooKeeperClusterKeyList;
    }


}
